package com.sgroupware.modules.leave.action;

import java.util.Date;
import java.util.List;

import javax.xml.registry.infomodel.User;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.opensymphony.xwork2.Preparable;
import com.simpleframe.domain.state.StateContext;
import com.simpleframe.domain.state.StateVo;
import com.simpleframe.web.action.CrudActionSupport;
import com.simplegovern.authority.SysGroup;
import com.simplegovern.authority.SysUser;
import com.simplegovern.authority.service.GroupService;
import com.sgroupware.modules.leave.Leave;
import com.sgroupware.modules.leave.service.LeaveService;

@Scope("prototype")
@Namespace("/leave")
@Action("/leave/leave")
public class LeaveAction extends CrudActionSupport  {
	@Autowired
	private LeaveService leaveService;
	@Autowired
	private GroupService groupService;
	private Leave leave;

	@Override
	public String main(){
		return MAIN;
	}
	
	@Override
	public String grid() throws Exception {
		page = leaveService.getPage(getSearcher());
		return GRID;
	}
	
	@Override
	public String create() throws Exception {
		leave = new Leave();
		leave.setProposerName(((SysUser)getSessionUser()).getTrueName());
		return EDIT;
	}
	
	@Override
	public String edit() throws Exception {
		
		return EDIT;
	}
	
	@Override
	public String save() throws Exception {
		leave.setProposerId(getSessionUser().getId());
		
		SysGroup group = groupService.getById(leave.getGroupId());
		leave.setGroupName(group.getName());
		leave.setInsertTime(new Date());
		leave.setState(2);//运行中
		leave.setStapName("branchAudtor");
		leave.setStapTitle("部门经理审核");
		
		leaveService.create(leave);
		return success("请假申请填写成功");
	}
	
	public String update() throws Exception {
		Leave update = leaveService.getById(leave.getId());
		leaveService.update(leave);
		return null;
	}
	
	protected void prepareUpdate() throws Exception {
		leave = leaveService.getById(getId());
	}
	
	public void validator() throws Exception {
		System.out.println("执行验证");
	}
	
	@Override
	public String delete() throws Exception {
		leaveService.deletes(ids);
		addMessage("删除成功");
		return null;
	}
	
	public List<SysGroup> getGroupList(){
		return groupService.getGroups();
	}
	
	public List<StateVo> getLeaveTypeList(){
		return StateContext.getItemsList("leaveType");
	}
	
	//setters and getters
	
	public void setLeave(Leave leave) {
		this.leave = leave;
	}
	
	public Leave getLeave() {
		return leave;
	}

}
